package com.boilerCloud.dao.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dao.sys.IHzlqswUserDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.account.AccountLoginRecordBean;
import com.boilerCloud.dto.account.LoginAccountBean;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.util.MD5;
import com.boilerCloud.util.StringUtil;


@Repository
public class HzlqswUserDaoImpl extends BaseDao implements IHzlqswUserDao {


	/**
	 * 帐号校验
	 * 
	 * @param account
	 * @param pwd
	 * @return
	 * @throws Exception
	 */
	public boolean checkUser(String account, String pwd) throws Exception {
		String sql = "select count(1) FROM lqsw_b2c_sys_user_base a WHERE account=? AND pwd=?  and is_delete ='n' ";
		if (this.jdbcTemplate.queryForObject(sql, new Object[] { account, pwd }, Integer.class) > 0) {
			return true;
		} else {
			return false;
		}
	}

	

	
	public User getUserInfoByAccount(String account) throws Exception {

		String sql = "select a.*"	
				+ " from lqsw_b2c_sys_user_base a  "
				+ " where a.account=? and  is_delete ='n' limit 1";

		try {
			return this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), account);
		} catch (IncorrectResultSizeDataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}




	@Override
	public int addAccount(String account, String name,String pwd, int businessNo)
			throws Exception {
		String sql = "insert into lqsw_b2c_sys_user_base(account,pwd,name,businessNo,is_delete) values(?,?,?,?,'n')";
		
		return this.jdbcTemplate.update(sql,account,pwd,name,businessNo);
	}




	@Override
	public void qryList(User account,
			HzlqswPage<User> page) throws Exception {
	String countsql = "select count(*) from lqsw_b2c_sys_user_base a "
			
			+ " where a.is_delete='n'  ";
		
		String listSql = "select a.* ,b.ROLENAME roleName"
				+ " from lqsw_b2c_sys_user_base a "
				+ "left join lqsw_b2c_sys_role b on a.role_id = b.id"
			
				+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(account.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + account.getName() +"%");
		}
		if(!StringUtil.isEmpty(account.getAccount())){
			countsql += " and a.account like ?";
			listSql += " and a.account like ?";
			params.add("%" + account.getAccount() +"%");
		}
		
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, User.class);
		
	}







	@Override
	public int delAccount(int id) throws Exception {
		String sql = "update lqsw_b2c_sys_user_base set is_delete ='y' where id=?";
		
		return this.jdbcTemplate.update(sql,id);
	}




	@Override
	public int resetPassword(int id, String pwd) throws Exception {
		String sql = "update lqsw_b2c_sys_user_base set pwd =? where id=?";
		
		return this.jdbcTemplate.update(sql,pwd,id);
	}




	@Override
	public void addUser(User user) throws Exception {
		String sql = "insert into lqsw_b2c_sys_user_base(account,name,pwd,status,create_time,"
				+ "modify_time,user_type,cell_phone,dealer_id,is_delete,role_id) values(?,?,?,1,now(),now(),?,?,?,'n',?)";
		List<Object> param = new ArrayList<Object>();
		param.add(user.getAccount());
		param.add(user.getName());
		param.add(user.getPwd());
		param.add(user.getUserType());
		param.add(user.getCellPhone());
		param.add(user.getDealerId());
		param.add(user.getRoleId());
		this.jdbcTemplate.update(sql,param.toArray());
		
		
	}




	@Override
	public void editUser(User user) throws Exception {
		String sql = "update lqsw_b2c_sys_user_base set modify_time = now() ";
				
		List<Object> param = new ArrayList<Object>();
		
		if(!StringUtil.isEmpty(user.getName())){
			sql+=" ,name=?";
			param.add(user.getName());
		}
		if(!StringUtil.isEmpty(user.getStatus())){
			sql+=" ,status=?";
			param.add(user.getStatus());
		}
		if(!StringUtil.isEmpty(user.getCellPhone())){
			sql+=" ,Cell_Phone=?";
			param.add(user.getCellPhone());
		}
		if(!StringUtil.isEmpty(user.getRoleId())){
			sql+=" ,role_id=?";
			param.add(user.getRoleId());
		}
		
		if(!StringUtil.isEmpty(user.getPwd())){
			sql+=" ,pwd=?";
			param.add(MD5.md5(user.getPwd()));
		}
		
		
		sql+=" where id=?";
		param.add(user.getId());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}


	@Override
	public User getUserByid(int id) throws Exception {
		String sql = "select a.* from lqsw_b2c_sys_user_base a where id= ? limit 1";
		return this.queryForObject(sql, User.class, id);
	}




	@Override
	public void editUserByDealerId(User user) throws Exception {
		String sql = "update lqsw_b2c_sys_user_base set name=?,"
				+ "modify_time=now(),cell_phone=? where dealer_id=?";
		List<Object> param = new ArrayList<Object>();
		param.add(user.getName());
		param.add(user.getCellPhone());
		param.add(user.getDealerId());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

	
	

	
}
